<template>
  <div class="group-buy-container">
    <div class="header">
      <div class="filter-container">
        <el-select v-model="filterStatus" placeholder="拼团状态" clearable style="width: 120px">
          <el-option label="拼团中" value="0" />
          <el-option label="成功" value="1" />
          <el-option label="失败" value="2" />
        </el-select>

        <el-input
            v-model="searchQuery"
            placeholder="请输入商品名称、ID"
            clearable
            style="width: 240px; margin-left: 10px"
            @keyup.enter="handleSearch"
        />

        <el-button type="primary" style="margin-left: 10px" @click="handleSearch">
          搜索
        </el-button>

        <el-button type="success" style="margin-left: 10px" @click="handleAdd">
          添加拼团商品
        </el-button>
      </div>
    </div>

    <el-table
        :data="tableData"
        border
        style="width: 100%; margin-top: 20px"
        v-loading="loading"
    >
      <el-table-column prop="id" label="ID" width="80" align="center" />
      <el-table-column prop="groupName" label="拼团名称" width="120" align="center" />
      <el-table-column prop="goodsName" label="商品名称" width="120" align="center" />
      <el-table-column label="商品图片" width="100" align="center">
        <template #default="{ row }">
          <el-image
              style="width: 60px; height: 60px"
              :src="row.goodsImg"
              :preview-src-list="[row.goodsImg]"
              fit="cover"
          />
        </template>
      </el-table-column>
      <el-table-column prop="groupDesc" label="拼团简介" width="180" />
      <el-table-column prop="groupPrice" label="拼团价" width="100" align="center">
        <template #default="{ row }">
          ¥{{ row.groupPrice }}
        </template>
      </el-table-column>
      <el-table-column prop="groupPeople" label="成团人数" width="100" align="center" />
      <el-table-column prop="participateNum" label="参与人数" width="100" align="center" />
      <el-table-column prop="endTime" label="结束时间" width="150" align="center">
        <template #default="{ row }">
          {{ row.endTime }}
        </template>
      </el-table-column>
      <el-table-column label="拼团状态" width="100" align="center">
        <template #default="{ row }">
          <el-tag :type="getStatusTagType(row.state)">
            {{ getStatusText(row.state) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150" align="center" fixed="right">
        <template #default="{ row }">
          <el-button size="small" @click="handleEdit(row)">编辑</el-button>
          <el-button size="small" type="danger" @click="handleDelete(row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
          v-model:current-page="form.pageNum"
          v-model:page-size="form.pageSize"
          :total="form.total"
          layout="total, sizes, prev, pager, next, jumper"
          :page-sizes="[10, 20, 30, 50]"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
      />
    </div>
  </div>
</template>

<script setup>
import { ref, onMounted } from 'vue'
import { ElMessage, ElMessageBox } from 'element-plus'
import axios from "axios";

// 表格数据
const tableData = ref([
  {
    id: 123,
    groupName: '李媛媛团',
    groupDesc: '新鲜西红柿拼团',
    groupUnit: '斤',
    startTime: new Date('2023-05-01'),
    endTime: new Date('2023-05-10'),
    groupValidity: 24,
    groupPeople: 20,
    buyNumLimitation: 5,
    sort: 1,
    state: '0',
    goodsAttribute: '有机',
    goodsDetails: '新鲜有机西红柿',
    createTime: new Date('2023-04-28'),
    isDelete: '0',
    createPeople: 'admin',
    groupPrice: 2.5,
    participateNum: 15,
    goodsId: 2,
    goodsName: '西红柿',
    goodsImg: 'https://via.placeholder.com/60x60?text=Tomato'
  },
  {
    id: 122,
    groupName: '王丽娜团',
    groupDesc: '新鲜黄瓜拼团',
    groupUnit: '斤',
    startTime: new Date('2023-05-02'),
    endTime: new Date('2023-05-11'),
    groupValidity: 24,
    groupPeople: 20,
    buyNumLimitation: 5,
    sort: 2,
    state: '0',
    goodsAttribute: '有机',
    goodsDetails: '新鲜有机黄瓜',
    createTime: new Date('2023-04-29'),
    isDelete: '0',
    createPeople: 'admin',
    groupPrice: 2.5,
    participateNum: 17,
    goodsId: 1,
    goodsName: '黄瓜',
    goodsImg: 'https://via.placeholder.com/60x60?text=Cucumber'
  }
])

// 分页相关
let form=ref({
  pageNum: 1,
  pageSize: 5
})
let pageNum = ref(1)
let pageSize = ref(10)
let total = ref(2)
let loading = ref(false)

// 搜索相关
const searchQuery = ref('')
const filterStatus = ref('')



// 状态显示
const getStatusText = (state) => {
  const statusMap = {
    '0': '拼团中',
    '1': '成功',
    '2': '失败'
  }
  return statusMap[state] || '未知'
}

const getStatusTagType = (state) => {
  const typeMap = {
    '0': 'warning',
    '1': 'success',
    '2': 'danger'
  }
  return typeMap[state] || 'info'
}

// 分页变化
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchData()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchData()
}

// 搜索
const handleSearch = () => {
  currentPage.value = 1
  fetchData()
}

// 添加
const handleAdd = () => {
  ElMessage.info('添加功能待实现')
}

// 编辑
const handleEdit = (row) => {
  ElMessage.info(`编辑ID为 ${row.id} 的记录`)
}

// 删除
const handleDelete = (row) => {
  ElMessageBox.confirm(`确定删除拼团 "${row.groupName}" 吗?`, '提示', {
    confirmButtonText: '确定',
    cancelButtonText: '取消',
    type: 'warning'
  }).then(() => {
    ElMessage.success('删除成功')
    // 这里实际调用删除API
  }).catch(() => {
    ElMessage.info('已取消删除')
  })
}

// 获取数据
const fetchData = () => {

  axios.post("/api/group/groupGoods/selectGroupGoods",form.value).then(
      (res)=>{
        if (res.data.code==200){
          tableData.value=res.data.data.list
          form.value.total=res.data.data.total
        }
      }
  )
}

onMounted(() => {
  fetchData()
})
</script>

<style scoped>
.group-buy-container {
  padding: 20px;
}

.header {
  margin-bottom: 20px;
}

.filter-container {
  display: flex;
  align-items: center;
}

.pagination-container {
  margin-top: 20px;
  display: flex;
  justify-content: flex-end;
}

.el-table {
  margin-top: 20px;
}

.el-image {
  cursor: pointer;
}
</style>
